package com.sure.controller.emp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.sure.common.dao.model.base.BaseRowMapper;
import com.sure.common.exception.SystemException;
import com.sure.common.service.model.BaseVO;
import com.sure.user.service.EmpService;
import com.sure.user.service.model.EmpQueryParamsVO;
import com.sure.user.service.model.EmpVO;

@Controller
@RequestMapping("/emp")
public class EmpController {
	
	/**
	 * @Desc 雇员Service
	 * @Author Sure
	 */
	@Autowired
	private EmpService empService;
	
	/**
	 * @Desc 返回页面的信息
	 * @Author Sure
	 */
	private String pageMessage;
	
	/**
	 * @Desc 跳转至雇员添加页面
	 * @Author Sure
	 * @Date 2015年1月2日 下午3:11:56
	 * @return
	 */
	@RequestMapping("toAdd")
	public String toAdd() {
		return "emp/empAdd";
	}
	
	/**
	 * @Desc 雇员信息添加
	 * @Author Sure
	 * @Date 2015年1月2日 下午3:12:17
	 * @param empVO
	 */
	@RequestMapping("/add")
	public String add(@ModelAttribute EmpVO empVO) {
		try {
			empService.add(empVO);
			return "common/success";
		} catch (SystemException e) {
			return "common/error";
		}
	}
	@RequestMapping(value="/testParam")
	public String testRequestParam(@RequestParam("name") String userName) {
		return "common/success";
	}
	@RequestMapping(value="/testModel")
	public String testModel(@RequestParam("name") String userName, Model model) {
		model.addAttribute("userName",userName);
		model.addAttribute("msg","Model传递到页面参数");
		return "common/success";
	}
	
	@RequestMapping(value="/testValid", method=RequestMethod.GET)
	public String testValid(@Valid EmpVO empVO, BindingResult bindingResult) {
		if(bindingResult.hasErrors()) {
			return "common/error";
		}
		return "common/success";
	}
	@RequestMapping(value="/path/{username}")
	public String testPath(@PathVariable String username, Model model) {
		model.addAttribute("msg", username);
		return "common/success";
	}
	
	@RequestMapping(value="query",produces="application/json")
	@ResponseBody
	public Map<String, Object> testJson(String deptNo) {
		Map<String, Object> map = new HashMap<String, Object>();
		EmpQueryParamsVO queryVO = new EmpQueryParamsVO();
		queryVO.setDeptNo(Integer.parseInt(deptNo));
		List<BaseVO> queryRes = null;
		try {
			queryRes = empService.query(queryVO, new BaseRowMapper(new EmpVO(),null));
			map.put("empList",queryRes);
			map.put("msg", "查询成功");
		} catch (SystemException e) {
			map.put("msg","雇员信息查询失败");
		}
		
		return map;
	}
	@RequestMapping("testUpload")
	@ResponseBody
	public Map<String, Object> testUpload(MultipartFile file){
		Map<String, Object> info = new HashMap<String, Object>();
		info.put("msg", "文件上传成功");
		return info;
		
	}
}
